package dao

import (
	_ "github.com/go-sql-driver/mysql"
	log "github.com/sirupsen/logrus"
	"simulationFulu/models"
)

//插入数据
func Add(product *models.Product) int64{
	affected, err := x.Insert(product)
	checkErr(err)
	return affected
}

//改
func Modify(id int, product *models.Product) bool {
	affected, err := x.Id(id).Cols(
		"ProductName",
		"ProductPrice",
		"HasCardNo",
		"HasCardPwd",
		"HasCardUrl").Update(product)
	checkErr(err)
	if affected == 0 {
		return false
	}
	return true
}

//删
func Remove(id int) bool{
	product := new(models.Product)
	affected, err:=x.Id(id).Delete(product)
	checkErr(err)
	if affected == 0 {
		return false
	}
	return true
}

//查
func Get(id int) *models.Product {
	product := &models.Product{Id: id}
	is, _ := x.Get(product)
	if !is {
		log.Warn("搜索结果不存在!")
		return nil
	}
	return product
}

func Query() []models.Product{
	var products []models.Product
	err := x.Table("product").
		Find(&products)
	checkErr(err)
	return products
}

func checkErr(err error) {
	if err != nil {
		log.Error("操作异常:", err)
		panic(err)
	}
}

